package soccer.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import soccer.helper.UrutKlubByNama;
import soccer.helper.UrutKlubByPoin;

public class Klasemen {
	private Map<String, Klub> database = new HashMap<String, Klub>();
	private List<Klub> terakhirMain = new ArrayList<Klub>();
	
	public void setTerakhirMain(List<Klub> terakhirMain) {
		this.terakhirMain = terakhirMain;
	}

	public void inputMatch(Integer klub1, Integer klub2, 
			Integer goal1, Integer goal2){
		List<Klub> daftarKlub = urutkanBerdasarkanNama();
		Integer pHome;
		Integer pAway;
		Klub k1 = daftarKlub.get(klub1-1);
		Klub k2 = daftarKlub.get(klub2-1);
		
		// update property object klub
		if(goal1 > goal2){
			pHome = 3;
			pAway = 0;
			k1.setWin(k1.getWin()+1);
			k2.setLose(k2.getLose()+1);
		}
		else if(goal1 < goal2){
			pHome = 0;
			pAway = 3;
			k1.setLose(k1.getLose()+1);
			k2.setWin(k2.getWin()+1);
		}
		else{
			pHome = pAway = 1;
			k1.setDraw(k1.getDraw()+1);
			k2.setDraw(k2.getDraw()+1);
		}
		k1.setPoint(k1.getPoint()+pHome);
		k1.setGoalsIn(k1.getGoalsIn()+goal1);
		k1.setGoalsOut(k1.getGoalsOut()+goal2);
		k1.setMatch(k1.getMatch()+1);
		k2.setPoint(k2.getPoint()+pAway);
		k2.setGoalsIn(k2.getGoalsIn()+goal2);
		k2.setGoalsOut(k2.getGoalsOut()+goal1);
		k2.setMatch(k2.getMatch()+1);
	}
	
	public Map<String, Klub> getDatabase() {
		return database;
	}

	public void setDatabase(Map<String, Klub> database) {
		this.database = database;
	}

	public List<Klub> getTerakhirMain() {
		return terakhirMain;
	}

	public List<Klub> urutkanBerdasarkanNama(){
		List<Klub> hasil = new ArrayList<Klub>(database.values());
		Collections.sort(hasil, new UrutKlubByNama());
		return hasil;
	}

	public List<Klub> urutkanBerdasarkanPoin(){
		List<Klub> hasil = new ArrayList<Klub>(database.values());
		Collections.sort(hasil, new UrutKlubByPoin());
		return hasil;
	}
}
